package com.mywang.weitravel.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.mywang.weitravel.R;
import com.mywang.weitravel.model.RedLocateListBean;
import com.mywang.weitravel.widget.JzvdStdMp3;

import java.util.ArrayList;
import java.util.List;

import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

public class RedLocateListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int type_mp3 = 1;

    private static final int type_mp4 = 2;

    private Context mContext;

    private List<RedLocateListBean> mData = new ArrayList<>();

    private RedMarkListener markListener;

    public RedLocateListAdapter(Context mContext, RedMarkListener listener) {
        this.mContext = mContext;
        this.markListener = listener;
    }

    public void setmData(List<RedLocateListBean> data) {
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    //点赞更新
    public void updateItem(int position) {
        mData.get(position).setIs_support(1);
        mData.get(position).setNumber(mData.get(position).getNumber() + 1);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType == type_mp3) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_list_red_locate_mp3, parent, false);
            return new RedLocateMp3Holder(view);
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_list_red_locate, parent, false);
            return new RedLocateHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        RedLocateListBean bean = mData.get(position);
        if (holder instanceof RedLocateHolder) {
            RedLocateHolder redLocateHolder = (RedLocateHolder) holder;
            redLocateHolder.jzvdStd.setUp(
                    mData.get(position).getUrl(),
                    mData.get(position).getTitle(), Jzvd.SCREEN_NORMAL);
            Glide.with(mContext).load(mData.get(position).getFilepath()).into(redLocateHolder.jzvdStd.thumbImageView);
            redLocateHolder.jzvdStd.thumbImageView.setAdjustViewBounds(true);
            redLocateHolder.jzvdStd.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);

            redLocateHolder.tv_title.setText(bean.getTitle());
            redLocateHolder.tv_num.setText(bean.getNumber() + "");
            if (bean.getIs_support() == 1) {
                redLocateHolder.iv_dianzan.setImageResource(R.mipmap.icon_video_dianzan_red);
            } else {
                redLocateHolder.iv_dianzan.setImageResource(R.mipmap.icon_video_dianzan_normal);
            }
            redLocateHolder.iv_dianzan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    markListener.markRed(position, bean.getId() + "");
                }
            });
        } else if (holder instanceof RedLocateMp3Holder) {
            RedLocateMp3Holder redLocateHolder = (RedLocateMp3Holder) holder;
            redLocateHolder.jzvdStd.setUp(
                    mData.get(position).getAudio_url(),
                    mData.get(position).getTitle(), Jzvd.SCREEN_NORMAL);
            Glide.with(mContext).load(mData.get(position).getFilepath()).into(redLocateHolder.jzvdStd.thumbImageView);
            redLocateHolder.jzvdStd.thumbImageView.setAdjustViewBounds(true);
            redLocateHolder.jzvdStd.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);

            redLocateHolder.tv_title.setText(bean.getTitle());
            redLocateHolder.tv_num.setText(bean.getNumber() + "");
            if (bean.getIs_support() == 1) {
                redLocateHolder.iv_dianzan.setImageResource(R.mipmap.icon_video_dianzan_red);
            } else {
                redLocateHolder.iv_dianzan.setImageResource(R.mipmap.icon_video_dianzan_normal);
            }
            redLocateHolder.iv_dianzan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    markListener.markRed(position, bean.getId() + "");
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mData.get(position).getAudio_url() != null && !mData.get(position).getAudio_url().trim().equals("")) {
            return type_mp3;
        } else {
            return type_mp4;
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RedLocateHolder extends RecyclerView.ViewHolder {

        JzvdStd jzvdStd;
        TextView tv_title;
        TextView tv_num;
        ImageView iv_dianzan;

        public RedLocateHolder(View itemView) {
            super(itemView);
            jzvdStd = itemView.findViewById(R.id.videoplayer);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_num = itemView.findViewById(R.id.tv_num);
            iv_dianzan = itemView.findViewById(R.id.iv_dianzan);
        }
    }

    class RedLocateMp3Holder extends RecyclerView.ViewHolder {

        JzvdStdMp3 jzvdStd;
        TextView tv_title;
        TextView tv_num;
        ImageView iv_dianzan;

        public RedLocateMp3Holder(View itemView) {
            super(itemView);
            jzvdStd = itemView.findViewById(R.id.videoplayer);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_num = itemView.findViewById(R.id.tv_num);
            iv_dianzan = itemView.findViewById(R.id.iv_dianzan);
        }
    }

    public interface RedMarkListener {
        void markRed(int position, String id);
    }
}
